//
//  BHCircleReviewModel.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/12.
//

import UIKit

class BHCircleReviewModel: NSObject {

    var reviewerId: String = "" // 评论人/回复人id
    var replyAvatar: String = ""
    var commentTime: String = ""  // 评论时间
    var parentId: String = ""
    var content: String = ""
    var reviewerName: String = "" // 评论人/回复人名称
    var replyName: String = ""  // 被回复人名称
    var replyId: String = "" // 被回复人id
    var reviewId: String = ""
    var circleId: String = ""
    var reviewerAvatar: String = ""
    
    var commentType: Int = 0 //  评论类型（1-评论动态  2-回复评论)'
    var authorFlag: Int = 0 // 是否作者 (0-否  1-是)
  
    
    var replys: [BHCircleReviewModel] = []
    
    // 本地记录
    var reviewFlag: Bool = false // 有更多评论的时候展开还是收起，默认收起
    
    
    
    func parseDic(_ dic: [String: Any])  {
        self.reviewerId = BHJsonTool.decodeStringFromDic(dic: dic, key: "reviewerId")
        self.replyAvatar = BHJsonTool.decodeStringFromDic(dic: dic, key: "replyAvatar")
        self.commentTime = BHJsonTool.decodeStringFromDic(dic: dic, key: "commentTime")
        self.parentId = BHJsonTool.decodeStringFromDic(dic: dic, key: "parentId")
        self.content = BHJsonTool.decodeStringFromDic(dic: dic, key: "content")
        self.reviewerName = BHJsonTool.decodeStringFromDic(dic: dic, key: "reviewerName")
        self.replyName = BHJsonTool.decodeStringFromDic(dic: dic, key: "replyName")
        self.replyId = BHJsonTool.decodeStringFromDic(dic: dic, key: "replyId")
        self.reviewId = BHJsonTool.decodeStringFromDic(dic: dic, key: "id")
        self.circleId = BHJsonTool.decodeStringFromDic(dic: dic, key: "circleId")
        self.reviewerAvatar = BHJsonTool.decodeStringFromDic(dic: dic, key: "reviewerAvatar")

        self.commentType = BHJsonTool.decodeIntFromDic(dic: dic, key: "commentType")
        self.authorFlag = BHJsonTool.decodeIntFromDic(dic: dic, key: "authorFlag")
        
        let replysArray = BHJsonTool.decodeArrayFromDic(dic: dic, key: "childrenCommentList")
        if replysArray.count != 0 {
            for childDic in replysArray {
                if childDic is NSNull {
                    continue
                }else {
                    let model = BHCircleReviewModel.init()
                    model.parseDic(childDic as! [String:Any])
                    self.replys.append(model)
                }
            }
        }
    }
    
    
    lazy var showAttribute: NSMutableAttributedString = {
        let attri = self.content.toAttribute().addLineSpace(8)
        attri.addFont(.systemFont(ofSize: 13, weight: .medium))
        attri.addAllTextColor(.color_222222)
        return attri
    }()
    
    lazy var cellHeight: CGFloat = {
        var height: CGFloat = 68.5
        let contentHeight = self.showAttribute.calculateSize(CGSize(width: UIScreen.width-82, height: CGFloat.greatestFiniteMagnitude)).height
        if self.replys.count != 0 {
            height = height+contentHeight+20
        }else {
            height = height+contentHeight
        }
        return height
    }()
    
    
    lazy var replyShowAttribute: NSMutableAttributedString = {
        if self.commentType == 2 {
            let replayContent = "回复"+" \(self.replyName):"+self.content
            
            let attri = replayContent.toAttribute()
            let style = NSMutableParagraphStyle.init()
            style.lineSpacing = 8
            style.lineBreakMode = .byCharWrapping
            attri.addAttributes([NSAttributedString.Key.paragraphStyle:style], range: NSRange.init(location: 0, length: replayContent.count))
            attri.addFont(.systemFont(ofSize: 13, weight: .medium))
            attri.addAllTextColor(.color_222222)
            attri.addSpecialTextColor(self.replyName, .color_999999)
            
            return attri
        }else {
            let attri = self.content.toAttribute()
            let style = NSMutableParagraphStyle.init()
            style.lineSpacing = 8
            style.lineBreakMode = .byCharWrapping
            attri.addAttributes([NSAttributedString.Key.paragraphStyle:style], range: NSRange.init(location: 0, length: self.content.count))
            attri.addFont(.systemFont(ofSize: 13, weight: .medium))
            attri.addAllTextColor(.color_444444)
            return attri
        }
    }()
    
    lazy var replyCellHeight: CGFloat = {
        var height: CGFloat = 45
        let contentHeight = self.showAttribute.calculateSize(CGSize(width: UIScreen.width-148, height: CGFloat.greatestFiniteMagnitude)).height
        height = height+contentHeight
        return height
    }()
    
    
    lazy var bottomHeight: CGFloat = {
        if self.replys.count > 2 {
            return 50
        }else {
            return 20
        }
    }()
    
}


class BHCircleReplyModel: NSObject {
    var reviewerId: String = "" // 评论人/回复人id
    var replyAvatar: String = ""
    var commentTime: String = ""  // 评论时间
    var parentId: String = ""
    var content: String = ""
    var reviewerName: String = "" // 评论人/回复人名称
    var replyName: String = ""
    var replyId: String = "" // 被回复人id
    var reviewId: String = ""
    var circleId: String = ""
    var reviewerAvatar: String = ""
    
    var commentType: Int = 0 //  评论类型（1-评论动态  2-回复评论)'
    var authorFlag: Int = 0 // 是否作者 (0-否  1-是)
    
    
    
    var iconUrl: String = ""
    var name: String = ""
    var time: String = ""
    var autherFlag: Int = 0  // 是否是发布人自己
    var replay: Int = 0 // 是否是回复某人
    var replayName: String = "" // 回复的是谁
    
    
    lazy var showAttribute: NSMutableAttributedString = {
        if self.replay == 0 {
            let attri = self.content.toAttribute().addLineSpace(8)
            attri.addFont(.systemFont(ofSize: 13, weight: .medium))
            attri.addAllTextColor(.color_444444)
            return attri
        }else {
            let replayContent = "回复"+" \(self.replayName):"+self.content
            
            let attri = replayContent.toAttribute().addLineSpace(8)
            attri.addFont(.systemFont(ofSize: 13, weight: .medium))
            attri.addAllTextColor(.color_222222)
            attri.addSpecialTextColor(self.replayName, .color_999999)
            
            return attri
        }
    }()
    
    lazy var cellHeight: CGFloat = {
        var height: CGFloat = 45
        let contentHeight = self.showAttribute.calculateSize(CGSize(width: UIScreen.width-148, height: CGFloat.greatestFiniteMagnitude)).height
        height = height+contentHeight
        return height
    }()
}
